import { siteList } from '@/api/site.js'
import store from '@/store'
import { getUrlItem } from '@/utils/index.js'
import i18n from '@/langs'
const getSiteList = function(params){
  return new Promise((resolve, reject) => {
    siteList(params).then(res=>{
      resolve(res)
    }).catch(res=>{
      reject(res)
    })     
  })
}
const commitSite = function(res, commit) {
  let data = res.data
  commit('SET_SITE_LIST', data)
  let list = JSON.parse(localStorage.getItem('site')) 
  if(list) {
    commit('SET_SITE_ICON', list.icon)
    commit('SET_LANG', list.siteLang)
    commit('SET_CUR_SITE', list)
  } else {
    let isIpsite = false
    for (let index = 0; index < data.length; index++) {
      const element = data[index];
      if (element.site_default){
        isIpsite = true
        // commit('SET_SITE_ICON', element.icon)
        // commit('SET_LANG', element.siteLang)
        // commit('SET_CUR_SITE', element)
        store.dispatch('ToggleLang', element)
        
      }
    }

    if(!isIpsite){
      commit('SET_SITE_ICON', data[0].icon)
      commit('SET_LANG', data[0].siteLang)
      commit('SET_CUR_SITE', data[0])
    }
    

  }
  store.dispatch('GenerateRoutes')
}

const site ={
  state: {
    siteList: [],
    lang: 'es',
    siteIcon: '',
    curSite: ''
  },
  mutations:{
    SET_SITE_LIST: (state, siteList) => {
      let obj = {
        date: new Date().getTime() + 5*60*1000,
        data: siteList
      }
      localStorage.setItem('siteList', JSON.stringify(obj))
      state.siteList = siteList;
    },
    SET_LANG:  (state, lang) => {
      // console.log(lang)
      state.lang = lang;
    },
    SET_SITE_ICON: (state, siteIcon) => {
      state.siteIcon = siteIcon;
    },
    SET_CUR_SITE: (state, curSite) => {
      localStorage.setItem('site', JSON.stringify(curSite))
      state.curSite = curSite;
    }
  },
  actions: {
    ToggleSite: ({ commit }) => {
      return new Promise((resolve) => {
        let site = getUrlItem('site') || ''
        let siteList = JSON.parse(localStorage.getItem('siteList')) 
        let params = {
          site :  site
        }
        if(siteList) {
          let oldDate = siteList.date
          let neWDate = new Date().getTime()
          if (neWDate < oldDate){
            commitSite (siteList, commit, site)
            resolve(true)
          }else {
            getSiteList(params).then(res=>{
              commitSite (res, commit, site)
              resolve(true)
            })
          }
        }else {
          getSiteList(params).then(res=>{
            commitSite (res, commit, site)
            resolve(true)
          })
        }
        store.dispatch('GetUserInfo')
      })
        
    },
    ToggleLang: ({commit}, data ) => {
      let site = data.site.toLowerCase()
      let langArr = ['us','de','es','fr','it','jp']
      if(langArr.indexOf(site) == -1){
        data.siteLang = 'us'
      }else {
        data.siteLang = site //使用的语言
      }
      localStorage.setItem('site', JSON.stringify(data))
      i18n.locale = data.siteLang
      commit('SET_LANG', data.siteLang)
      commit('SET_SITE_ICON', data.icon)
      commit('SET_CUR_SITE', data)
      store.dispatch('GenerateRoutes')
    }
  }
}

export default site